/**
*@author: Bruno Fagundez @ 05-30-2008
*/
public without sharing class DiscussionOverlayTopicEdit {
   	
   	private DiscussionTopic Topic = new DiscussionTopic(); 
	
	public String TEAMID = System.currentPageReference().getParameters().get('id');
	public DiscussionForum__c forum = new DiscussionForum__c();
	
	/**
	* Constructor
	*/
	public DiscussionOverlayTopicEdit () {}
	
	/**
	* Get Team__c Id
	*@return String
	*/
	public String getTeamId () {
		return TEAMID;
	}

	/**
	* Method for do Dummy refresh.
	*/
	public void dummyRefresh(){}
		
	/**
	* Get the forum data 
	*@return DiscussionForum__c
	*/	
	public DiscussionForum__c getForumData(){
   		if(TEAMID != null){
   			 try {
   			 	forum = [SELECT
   						Name, 
   						Id 
   						FROM DiscussionForum__c WHERE Id =: TEAMID LIMIT 1];
   			 } catch (Exception e) {
   			 	throw e;
   			 }   			    			 
   		} else {
   			forum = null;
   		}
   		return forum;
   	}   	
   	   	
   	/**
   	* Get DiscussionTopic Instance
   	*@return DiscussionTopic
   	*/
   	public DiscussionTopic getTopic(){
   		return Topic;
   	}
   	
   	/**
   	* Set DiscussionTopic instance
   	*@param DiscussionTopic
   	*/
   	public void setTopic (DiscussionTopic dt){
   		Topic = dt;
   	}
   	
   	/**
	* Read current Forum and Save the topic
	*/   	
   	public void saveTopic(){
   		//Read forum
   		getForumData();
   		// Build and Insert the Topic first
   		DiscussionTopic__c newTopic = new DiscussionTopic__c();
   		newTopic.Subject__c = Topic.Name;	
		newTopic.DiscussionForum__c = forum.Id;
		newTopic.ViewCount__c = 0.0;
   		insert newTopic;
   		
   		// Build and insert First Message 
   		DiscussionMessage__c firstMessage = new DiscussionMessage__c();
   		firstMessage.DiscussionTopic__c = newTopic.Id;
   		firstMessage.Discussion__c = TEAMID;
		firstMessage.Message__c = Topic.Text;
		firstMessage.PostedBy__c = UserInfo.getUserId();
   		insert firstMessage;
   	}

   	/**
	* Read current Forum and Update the topic
	*/   	
   	public void updateTopic(){
   		// Update the Topic 
   		DiscussionTopic__c topicToEdit = [Select Subject__c from DiscussionTopic__c where Id =: Topic.TopicIdToUpdate];
   		topicToEdit.Subject__c = Topic.Name;	
   		update topicToEdit;
   		// Update the topic First Message 
   		DiscussionMessage__c messageToUpdate = [ Select Message__c from DiscussionMessage__c where Id =: Topic.MessageIdToUpdate ];
   		messageToUpdate.Message__c = Topic.Text;			
   		update messageToUpdate;
   	}

	/**
	* DiscussionTopic class
	*@author Bruno Fagundez
	*/	
   	public class DiscussionTopic {   
   		public String TopicIdToUpdate {get; set;}	
   		public String MessageIdToUpdate {get; set;}
   		public String Name {get; set;}
   		public String Text {get; set;}
   	}
}